package com.project.frba.bi.transformation;

import java.util.ArrayList;
import java.util.List;

import com.project.frba.bi.descriptor.Descriptor;
import com.project.frba.bi.descriptor.Dimension;
import com.project.frba.bi.extraction.ExtractionResult;


public class ProcessorImpl implements Processor {
	
	private FactTable factTable;
	private List<DimensionTable> dimensionTables = new ArrayList<DimensionTable>();
	

	public ProcessorImpl(Descriptor descriptor) {
		factTable = new FactTable(descriptor);
		this.buildDimensionTables();
	}
	
	
	private void buildDimensionTables(){
		for (Dimension dimension : factTable.getDescriptor().getDimensions()) {
			DimensionTable dimensionTable = new DimensionTable(dimension);
			dimensionTables.add(dimensionTable);
		}
	}
	
	public void process(ExtractionResult result){
		while(true){
			Long factRowId = this.factTable.process(result);
			for(DimensionTable dimensionTable: dimensionTables){
				Long rowId = dimensionTable.process(result);
				factTable.addField(factRowId, dimensionTable.getDimensionName(), rowId);
			}
			if(!result.hasNext()){
				break;
			}
			result.next();
		}
	}

	public List<DimensionTable> getDimensionTables() {
		return dimensionTables;
	}

	public void setDimensionTables(List<DimensionTable> dimensionTables) {
		this.dimensionTables = dimensionTables;
	}
	
	public void setFacttable(FactTable factTable) {
		this.factTable = factTable;
	}

	public FactTable getFactTable() {
		return this.factTable;
	}

	public TransformationResult getTransformationResult() {
		return new TransformationResultImpl(this.factTable, this.dimensionTables);
	}

}
